package day0724;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.Random;

public class Test1 {
    public static void main(String[] args) {
        JFrame f = new JFrame("飞机大战");
        GamePane pane = new GamePane();
        f.add(pane);

        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(GamePane.width, GamePane.height);
        f.setLocationRelativeTo(null);
        f.setVisible(true);
        pane.startGame();
    }
}


class GamePane extends JPanel {

    static int height = 700;
    static int width = 420;

    public void startGame() {
        while(true){
            createFlyObjects();
            createBullets();
            isHited();
            //System.out.println(planes.size());
            moveAction();
            repaint();
            try {
                Thread.sleep(110);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    




    private void isHited() {
        for (int i = 0; i < planes.size(); i++) {
            FlyingObject plane=planes.get(i);
            int x=plane.x;
            int x1=plane.x+plane.width;
            int y=plane.y;
            int y1=plane.y+plane.height;
            for (int j = 0; j < bullets.size(); j++) {
                FlyingObject bullet=bullets.get(j);
                int x2=plane.x-bullet.width;
                int y2=plane.y-bullet.height;
                if(x2<x&x<x1&y2<y&y<y1){
                    planes.remove(plane);
                    bullets.remove(bullet);
                }
            }
        }
    }






    private void moveAction() {
        for (int i = 0; i < planes.size(); i++) {
            FlyingObject f=planes.get(i);
            f.fly();
        }
        for (int i = 0; i < bullets.size(); i++) {
            FlyingObject f=bullets.get(i);
            f.fly();
        }
    }

    HeroPlane heroPlane=new HeroPlane();
    ArrayList<FlyingObject> bullets=new ArrayList<>();
    int bulletIndex=1;
    public void createBullets() {
        if (bulletIndex%7==0){
            Bullet[] bs=heroPlane.shoot();
            for (int i = 0; i < bs.length; i++) {
                bullets.add(bs[i]);
            }
        }
        bulletIndex++;
    }

    ArrayList<FlyingObject> planes=new ArrayList<>();
    int planeIndex=1;
    public void createFlyObjects(){
        if (planeIndex%10==0){
            Random r=new Random();
            int n=r.nextInt(20);
            if (0<=n&n<10){
                Beeplane b=new Beeplane();
                planes.add(b);
            }else if (10<=n&n<=20){
                Airplane a=new Airplane();
                planes.add(a);
            }
        }
        planeIndex++;
    }

    public void paint(Graphics g) {//自动执行
        ImageIcon bgIcon = new ImageIcon("images1/background.png");
        g.drawImage(bgIcon.getImage(), 0, 0, null);
        for (int i = 0; i < planes.size(); i++) {
            FlyingObject fly = planes.get(i);
            g.drawImage(fly.beeImage, fly.x, fly.y,null);
            g.drawImage(fly.airplaneImage, fly.x, fly.y,null);///////////////////////////////
        }
        for (int i = 0; i < bullets.size(); i++) {
            FlyingObject fly = bullets.get(i);
            g.drawImage(fly.bulletImage, fly.x, fly.y,null);
        }
    }
}







